package com.umeng.soexample.mvp.view.fragment;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.umeng.lib_core.base.BaseFragment;
import com.umeng.soexample.R;
import com.umeng.soexample.bean.CategoryLeftEntity;
import com.umeng.soexample.bean.CategoryRightEntity;
import com.umeng.soexample.di.compont.DaggerCategoryFragmentCompont;
import com.umeng.soexample.di.modules.CategoryFragmentModules;
import com.umeng.soexample.mvc.view.FragmentActivity;
import com.umeng.soexample.mvp.contract.CategoryFragmentContract;
import com.umeng.soexample.mvp.persenter.CategoryFragmentPresenter;
import com.umeng.soexample.mvp.view.adapter.CategoryLeftAdapter;
import com.umeng.soexample.mvp.view.adapter.CategoryRightAdapter;

/**
 * 品类页面
 */
public class CategoryFragment extends BaseFragment<CategoryFragmentPresenter> implements CategoryFragmentContract.CategoryFragmentIView, BaseRefreshListener {

    private String categoryid = "1";

    private View view;
    private RecyclerView rv_category_left;
    private PullToRefreshLayout ptrl_category;
    private RecyclerView rv_category_right;

    private CategoryLeftAdapter leftAdapter;
    private CategoryRightAdapter rightAdapter;


    @Override
    public View createView(LayoutInflater inflater) {
        view = inflater.inflate(R.layout.fragment_category, null);
        return view;
    }

    @Override
    public void init(View view) {
        rv_category_left = view.findViewById(R.id.rv_category_left);
        rv_category_left.setLayoutManager(new LinearLayoutManager(getActivity()));
        leftAdapter = new CategoryLeftAdapter(R.layout.item_category_left);
        rv_category_left.setAdapter(leftAdapter);

        ptrl_category = view.findViewById(R.id.ptrl_category);
        ptrl_category.setCanLoadMore(false);

        rv_category_right = view.findViewById(R.id.rv_category_right);
        rv_category_right.setLayoutManager(new GridLayoutManager(getActivity(), 3, GridLayoutManager.VERTICAL, false));
        rightAdapter = new CategoryRightAdapter(R.layout.item_category_right);
        rv_category_right.setAdapter(rightAdapter);
    }

    @Override
    public void initData() {
        p.requestAll();
    }

    @Override
    public void setupDagger() {
        DaggerCategoryFragmentCompont.builder().categoryFragmentModules(new CategoryFragmentModules(this))
                .build().inject(this);
    }

    @Override
    public void initListener() {
        ptrl_category.setRefreshListener(this);
        //左侧列表点击事件
        leftAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < leftAdapter.getData().size(); i++){
                    leftAdapter.getData().get(i).isSelector = false;
                }
                leftAdapter.getData().get(position).isSelector = true;
                leftAdapter.notifyDataSetChanged();
                categoryid = leftAdapter.getData().get(position).getCategory_id();
                p.requestRight(categoryid);


            }
        });


        //底部数据点击事件
//        leftAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Intent goodsValues = new Intent(getActivity(), FragmentActivity.class);
//                goodsValues.putExtra("type", "goods_values");
//                goodsValues.putExtra("goods_id", mineAdapter.getData().get(position).goods_id);
//                getActivity().startActivity(goodsValues);
//            }
//        });
        //右侧数据点击事件
        rightAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
               // Toast.makeText(getActivity(), ((CategoryRightAdapter)adapter).getData().get(position).getName(), Toast.LENGTH_SHORT).show();
//                Intent goodsValues = new Intent(getActivity(), FragmentActivity.class);
//                goodsValues.putExtra("type", "goods_values");
//                goodsValues.putExtra("goods_id", rightAdapter.getData().get(position).getCategory_id());
//                getActivity().startActivity(goodsValues);

                Intent shop_details = new Intent(getActivity(), FragmentActivity.class);
                shop_details.putExtra("type", "shop_details");
                getActivity().startActivity(shop_details);
            }
        });
    }

    @Override
    public void categoryLeftSuccess(CategoryLeftEntity entity) {
        Log.e("tt", "CategoryFragment -> categoryLeftSuccess: -> " + entity.getValues().size());
        entity.getValues().get(0).isSelector = true;
        leftAdapter.addData(entity.getValues());
    }

    @Override
    public void categoryRightSuccess(CategoryRightEntity entity) {
        Log.e("tt", "CategoryFragment -> categoryRightSuccess: -> " + entity.getValues().size());
        if (entity == null || entity.getValues().size() == 0){
            return;
        }
        rightAdapter.getData().clear();
        rightAdapter.addData(entity.getValues());
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }


    @Override
    public void refresh() {
        ptrl_category.finishRefresh();
        p.requestRight(categoryid);
    }

    @Override
    public void loadMore() {
        ptrl_category.finishLoadMore();
    }
}
